//获取传过来的id
var id = location.search.split('?id=')[1];
// console.log(id);


$.ajax({
    "url": '../演艺项目json/goodDetail.json',
    // "url": "https://www.fastmock.site/mock/40e170f960701a834866a0bc956002f6/tickets/getAllArts",
    "success": function (data) {
        var arr = data.goods;
        // console.log(arr);
        // 筛选出id相符合的对象
        var arrObj = arr.filter(e => e.id == id)[0];
        console.log(arrObj);

        //处理数据到页面
        additem(arrObj);


        //选演出时间
        $('#select-time').on('click', '.time', function () {
            $(this).css('border', '1px solid #ff3c1b');
            $(this).siblings().css('border', '');
        })
        var clickPrice1 = false;
        //选价格修改obj
        $('.pay').on('click', 'div', function () {
            $(this).css('border', '1px solid #ff3c1b');
            $(this).siblings().css('border', '');
            var str = $(this).text();
            var reg = /\d+/g;
            var num = str.match(reg)[0];
            //获取当前点击票价
            console.log(num);

            //添加到对象 新属性
            arrObj.clickPrice = num;
            clickPrice1 = true;
        })

        //点击购买按钮
        $('#chooseBtn').click(function () {
            // console.log(arrObj);
            if (clickPrice1) {
                //储存对应信息到本地
                addData('payDetail', arrObj);
                localStorage.removeItem("seat");
                location.href = '../html/chooseSeat.html'
            } else {
                alert('没有选择时间票价');
            }


        })

        //百度地图
        //点击地图事件
        $('.clickMap').on('click', function () {
            var p = $('.showPlace').text();
            console.log(p);
            newMap(p);
            $('#result').text(p);
            $('.baidu').show();
        });
        //隐藏地图
        $('#closeMap').click(function () {
            $('.baidu').hide();
        });
    }
});


function additem(e) {
    //e是对象
    //添加照片
    $('.movies-img').attr('src', `${e.img_src}`);
    //添加标题
    $('#content-nav-a').text(e.good_name);
    $('.content-left h1').text(e.good_name);

    //拆分字符
    var reg = /《|》|“|”/;
    var title = e.good_name.split(reg)[1];
    console.log(title);
    $('.content-left p').text(title);

    //添加时间
    var timeArr = e.times;
    timeArr.forEach((v, i) => {
        $('#select-time ').append(`<div class="time">${v}</div>`);
    });
    $('.showTime').text(timeArr[0])
    // console.log(timeArr);

    //地点
    $('.showPlace').text(e.place);

    //价格
    if (typeof e.price == 'string') {
        $('.pay').append(`<div>${e.price}元</div>`);
    } else {
        e.price.forEach(v => {
            $('.pay').append(`<div>${v}</div>`);
        })
    }
    //项目概况
    $('#total-detail').append(e.details)

}

//获取本地数据
function getData(name) {
    var local = localStorage.getItem(name);
    if (local != null) {
        return JSON.parse(local);
    } else {
        return [];
    }
}
//添加本地信息
function addData(name, data) {
    localStorage.setItem(name, JSON.stringify(data));
}




loadComment();
//评论读取
function loadComment() {
    var comments = getData('comment');
    for (var i = 0; i < comments.length; i++) {
        var v = comments[i];
        if (comments[i].movieId == id) {
            $('#list').append(`<div class="item">
        <div class="user">
        <img src="../img/img-details/user.png" alt="">
        <p>${v.userPhone}</p>
        </div>
        <div class="comment">
        <p id='commentNew'>${v.content}</p>
        <div class="count ${v.star}"></div>
        <span class="date"></span>
        </div>
        </div>`);

        }
    }


}



//点击事件
//评论样式更改
$('#comment').on('click', 'li', function () {
    var class1 = $(this).attr('class');
    $(this).parent().attr('class', `${class1}`);
});


//评论点击生成
$('#commentBtn').click(function () {

    var local = getData('login');
    console.log(local);
    //判断登录状态
    if (local.user != undefined) {
        //获取上面标签点击star标签
        var n = $('#comment ul').attr('class');

        //获取texarea的文字
        var text = $('#texta').val();
        // console.log(text);

        //判断是否输入了评论内容
        if (text != '') {
            //添加评论
            $('#list').append(`<div class="item">
         <div class="user">
         <img src="../img/img-details/user.png" alt="">
         <p>${local.user}</p>
         </div>
         <div class="comment">
         <p id='commentNew'>${text}</p>
         <div class="count ${n}"></div>
         <span class="date"></span>
         </div>
         </div>`);
            //获取本地数据
            var comments = getData('comment');
            //获取新添加的评论
            var newCom = $('#list').children().last();
            //处理数据保存对象
            var obj = {};
            obj.userPhone = local.user;
            obj.movieId = id;
            obj.star = n;
            obj.content = newCom.children().children('#commentNew').text();
            //添加新的到comments
            comments.push(obj);
            addData('comment', comments);
        } else {
            alert('请先输入评论内容')
        }



    } else {
        alert('请先登录');
    }

});